package com.example.todolist.controller;

import com.example.todolist.common.Result;
import com.example.todolist.model.Todo;
import com.example.todolist.service.ITodoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/todos")
@CrossOrigin(origins = "*")
public class TodoController {

    @Autowired
    private ITodoService todoService;

    @GetMapping
    public Result<List<Todo>> getAllTodos() {
        log.info("接收到获取所有待办事项的请求");
        return Result.success(todoService.getAllTodos());
    }

    @PostMapping
    public Result<Todo> createTodo(@RequestBody Todo todo) {
        log.info("接收到创建待办事项的请求: {}", todo);
        return Result.success(todoService.createTodo(todo));
    }

    @PutMapping("/{id}")
    public Result<Todo> updateTodo(@PathVariable Long id, @RequestBody Todo todo) {
        log.info("接收到更新待办事项的请求, ID: {}", id);
        return Result.success(todoService.updateTodo(id, todo));
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteTodo(@PathVariable Long id) {
        log.info("接收到删除待办事项的请求, ID: {}", id);
        todoService.deleteTodo(id);
        return Result.success();
    }
} 